<?php

class PC4_Controlador_Crud_Usuarios extends TICE_Controlador_Crud {

	public $campoSenha = 'senha',
			$campoTipo = 'role',
			$md5 = true;
	
	function __construct(Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response, array $invokeArgs = array() ) {
		parent::__construct($request, $response, $invokeArgs);
		$this->prependPastaView('usuarios');
	}
	public function init()
    {
        $this->tabela = 'usuarios';
        $this->nomeSecao = 'Usuários';
    	$this->campos = array(
    		'id', 
    		'nome', 
    		'usuario' => 'Usuário', 
    		$this->campoSenha => array(
    			'view' => 'password',
    			'ocultoLista' => true
    		)
    	);	
    }

    protected function getBean($id = 0) {
    	$bean = parent::getBean($id);
    	if (!empty($bean[$this->campoSenha])) {
    		if ($this->md5) {
    			$bean[$this->campoSenha] = md5($bean[$this->campoSenha]);
    		}
    	}
    	else {
    		unset($bean[$this->campoSenha]);
    	}
    	if (empty($bean[$this->campoTipo])) {
    		$bean[$this->campoTipo] = 'admin';
    	}
    	return $bean;
    }
    
    protected function criaTabela() {
    	$sql =
			"CREATE TABLE IF NOT EXISTS `" . $this->tabela . "` (
			  `id` INT NOT NULL AUTO_INCREMENT ,
			  `nome` VARCHAR(60) NULL ,
			  `email` VARCHAR(45) NULL ,
			  `usuario` VARCHAR(60) NULL ,
			  `" . $this->campoSenha . "` VARCHAR(34) NULL ,
			  `role` VARCHAR(20) NULL ,
			  `ativo` TINYINT(1) NULL DEFAULT 1 ,
			  `registro` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ,
			  PRIMARY KEY (`id`) )
			ENGINE = InnoDB";
    	$db = $this->modelo->getAdapter();
    	$db->query($sql);
    	if ($this->modelo->fetchRow() == null) {
	    	$this->modelo->insert(array(
	    		'nome' => 'PC4',
	    		'usuario' => 'pc4',
	    		'senha' => '909634296908deb670d65206990e290d',
	    		'role' => 'admin',
	    		'email' => 'manutencao@pc4.com.br'
	    	));
    	}
    }

}

